package cn.usian.controller;

import cn.usian.common.R;
import cn.usian.entity.Employee;
import cn.usian.service.EmployeeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RequestMapping("/employee")
@RestController

public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @RequestMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        R<Employee> r = employeeService.login(employee);
        //成功
        if (r.getCode()==1){
            //将用户的编号存在session中
            session.setAttribute("employee",r.getData().getId());
        }
        return r;
    }
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("退出成功");
    }
    @RequestMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        return employeeService.page(page,pageSize,name);
    }
    @PostMapping
    public R save(@RequestBody Employee employee,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        employeeService.save(employee);
        return R.success("添加成功");
    }
    @PutMapping
    public R updateEmp(@RequestBody Employee employee){
        employeeService.updateEmp(employee);
        return R.success("修改成功");
    }
    @GetMapping("{id}")
    public R findByid(@PathVariable Long id){
        return employeeService.findByid(id);
    }
}
